﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Stg;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Stg
{
    public class stg_tblContactPersonDAL:Istg_tblContactPerson
    {
        HKEA_CMSEntities model;
        stg_tblContactPerson person;

        public stg_tblContactPersonDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static stg_tblContactPersonDAL GetInstance()
        {
            return new stg_tblContactPersonDAL();
        }

        public int InsertENT(stg_tblContactPerson ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.stg_tblContactPerson.AddObject(ent);
            return model.SaveChanges();
        }

        public List<stg_tblContactPerson> GetListByENT(stg_tblContactPerson ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.stg_tblContactPerson.Where(a => a.ContactPersonUID > 0);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<stg_tblContactPerson> GetListByENT(stg_tblContactPerson ent)
        {
            var result = model.stg_tblContactPerson.Where(a => a.ContactPersonUID > 0);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(stg_tblContactPerson ent)
        {
            var result = model.stg_tblContactPerson.Where(a => a.ContactPersonUID > 0);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(stg_tblContactPerson ent)
        {
            person = model.stg_tblContactPerson.Where(a=>a.ContactPersonUID==ent.ContactPersonUID).FirstOrDefault();
            if (person != null)
            {
                person.LastModifyBy = ent.LastModifyBy;
                person.LastModifyDate = DateTime.Now;
                person.ActivityUID = ent.ActivityUID;
                person.AddressFollowCust = ent.AddressFollowCust;
                person.AddressUID = ent.AddressUID;
                person.AddressUID_CP = ent.AddressUID_CP;
                person.AddressUID_Cust = ent.AddressUID_Cust;
                person.ChristianName = ent.ChristianName;
                person.ContactInfoFollowCust = ent.ContactInfoFollowCust;
                person.ContactInfoUID = ent.ContactInfoUID;
                person.ContactInfoUID_CP = ent.ContactInfoUID_CP;
                person.ContactInfoUID_Cust = ent.ContactInfoUID_Cust;
                person.FirstName_Chi = ent.FirstName_Chi;
                person.FirstName_Eng = ent.FirstName_Eng;
                person.HKID_PassportID = ent.HKID_PassportID;
                person.LastName_Chi = ent.LastName_Chi;
                person.LastName_Eng = ent.LastName_Eng;
                person.OtherTitle_Chi = ent.OtherTitle_Chi;
                person.OtherTitle_Eng = ent.OtherTitle_Eng;
                person.Position = ent.Position;
                person.RelatedContactPersonUID = ent.RelatedContactPersonUID;
                person.Relationship = ent.Relationship;
                person.Remark = ent.Remark;
                person.SalutationUID = ent.SalutationUID;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(stg_tblContactPerson ent)
        {
            var temp = model.stg_tblContactPerson.Where(a => a.ContactPersonUID > 0);
            if (ent.ContactPersonUID > 0)
                temp = temp.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.stg_tblContactPerson.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
